package com.beemans.common.app.ui.fragments

import android.os.Bundle
import android.util.Log
import android.view.View
import com.beemans.common.app.R
import com.beemans.common.app.databinding.FragmentGlideBinding
import com.beemans.common.app.ui.activities.TestActivity
import com.beemans.common.app.ui.base.BaseFragment
import com.beemans.common.ext.*
import com.blankj.utilcode.util.ActivityUtils
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.http.imageloader.imgCtxWrap
import com.tiamosu.fly.imageloader.glide.BlurTransformation
import com.tiamosu.fly.utils.launchMain

/**
 * @author tiamosu
 * @date 2021/5/28.
 */
class GlideFragment : BaseFragment() {
    private val dataBinding: FragmentGlideBinding? by lazyDataBindingOrNull { }

    private var title: String? = null

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_glide)
    }

    override fun initParameters(bundle: Bundle?) {
        title = getString(MainFragment.PARAM_TITLE)
    }

    override fun initView(rootView: View?) {
        dataBinding?.glideTitleBar?.setTvTitle { text = title }

        IMG_URL.getBitmap(imgCtxWrap, block = {
            transform(BlurTransformation())
        }) {
            onSuccess { resource, _, _, _, _ ->
                Log.e("susu", "onSuccess --- getBitmap")
                launchMain {
                    dataBinding?.glideIvCircle?.setImageBitmap(resource)
                }
            }
            onFail { _, _, _, _ ->
                Log.e("susu", "onFail --- getBitmap")
            }
        }

//        dataBinding.glideIvCircle.loadCircleImage(IMG_URL) {
//            options {
//                Log.e("susu", "options")
//            }
//            onSuccess { _, _, _, _, _ ->
//                Log.e("susu", "onSuccess --- loadCircleImage")
//            }
//            onFail { _, _, _, _ ->
//                Log.e("susu", "onFail --- loadCircleImage")
//            }
//        }

        dataBinding?.glideIvGif?.loadImage(R.drawable.timg) {
            options {
                centerCrop()
                imageRadius(20.pt2px)
            }
            onSuccess { _, _, _, _, _ ->
                Log.e("susu", "onSuccess --- loadImage")
            }
            onFail { _, _, _, _ ->
                Log.e("susu", "onFail --- loadImage")
            }
        }
    }

    override fun initEvent() {
        dataBinding?.glideTitleBar?.setPageBack()
    }

    override fun doBusiness() {
//        jumpFragmentTest()
    }

    /**
     * Fragment 页面跳转测试
     */
    private fun jumpFragmentTest() {
        postDelayed({
            Log.e("susu", "start")
            jumpFragment(R.id.httpFragment)
        }, 2000)

        ActivityUtils.startActivity(TestActivity::class.java)
//        jumpFragment(R.id.customViewFragment)
    }

    companion object {
        const val IMG_URL =
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fdik.img.kttpdq.com%2Fpic%2F19%2F12786%2F67278f953e503402_1024x768.jpg&refer=http%3A%2F%2Fdik.img.kttpdq.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618040074&t=84d6ac67232e3da6d965e3e60dce87e6"
    }
}